var objId =  getQueryString("objId");
//var orderIds =  getQueryString("orderIds")
var $;
layui.config({
	base : "js/"
}).use(['form','layer','jquery'],function(){
	var form = layui.form(),
		layer = parent.layer === undefined ? layui.layer : parent.layer,
		laypage = layui.laypage;
		$ = layui.jquery;

	   getPageList();
	   
	   
	   $("body").on("click",".users_del",function(){  //删除
		var _this = $(this);
		layer.confirm('确定移除当前订单？',{icon:3, title:'提示信息'},function(index){
			//_this.parents("tr").remove();
			var  data = 
			{
				"orderIds":_this.attr("data-id"),
				"id":objId
			}
			$.ajax({
			        url: API_URL_HEAD+"outPutBills/deleteBillsOrderById",
			        type: 'POST',
			        async: false,
			        data :JSON.stringify(data),
			        contentType: 'application/json; charset=utf-8',
			        dataType: 'json',
			        success: function (data) {
						
						layer.msg('移除成功！', {icon: 1});
						getPageList();
						layer.close(index);
			        }
			 });
		});
	})
    //加载页面数据
	function renderData(data){
		$(".vo_content").empty();
		var dataHtml = '';
		if(data.length != 0){
			for(var i=0;i<data.length;i++){
				
				var payTypeHtml = ['0','现金','水票','微信','支付宝','银联','其他','一卡通'];
				var statusHtml = ['0','待支付','已支付','配送中','完成','取消'];
				var controlHtml = '';
				if(data[i].status == '3')
				{
					controlHtml = '<a class="layui-btn layui-btn-danger layui-btn-mini users_del" data-id="'+data[i].id+'"><i class="layui-icon">&#xe640;</i>移除订单</a>';
				}
				dataHtml += '<tr>'
//			    	+  '<td><input type="checkbox" name="checked" lay-skin="primary" data-id="'+data[i].id+'" lay-filter="choose"></td>'
			    	+  '<td>'+data[i].orderSn+'</td>'
			    	+  '<td>'+data[i].waterName+'</td>'
			    	+  '<td>'+data[i].studentName+'</td>'
			    	+  '<td>'+data[i].waterNum+'</td>'
			    	+  '<td>'+data[i].recycleBucketNum+'</td>'
			    	+  '<td>'+data[i].waterPrice+'</td>'
			    	+  '<td>'+data[i].roomName+'</td>'
			    	+  '<td>'+data[i].studentAdder+'</td>'
			    	+  '<td>'+handleNullToNullString(data[i].deliveryName)+'</td>'
			    	+  '<td>'+data[i].placeOrderTime+'</td>'
			    	+  '<td>'+data[i].payAmount+'</td>'
			    	+  '<td>'+payTypeHtml[data[i].payType]+'</td>'
			    	+  '<td>'+handleNullToNullString(data[i].payTime)+'</td>'
			    	+  '<td>'+statusHtml[data[i].status]+'</td>'
//			    	+  '<td>'+data[i].remark+'</td>'
			    	+  '<td>'
//					+    '<a class="layui-btn layui-btn-mini users_edit" data-id="'+data[i].id+'"><i class="iconfont icon-edit"></i> 编辑</a>'
					+   controlHtml 
			        +  '</td>'
			    	+'</tr>';
			}
		}else{
			dataHtml = '<tr><td colspan="14">暂无数据</td></tr>';
		}
		$(".vo_content").html(dataHtml);
		form.render();
	}
	
	function getPageList(){
		var data={
			"id":objId
		};
	    $.ajax({
	        url: API_URL_HEAD+"orderWater/selectListByOrderId",
	        type: 'post',
	        async: false,
	        data :JSON.stringify(data),
	        contentType: 'application/json; charset=utf-8',
	        dataType: 'json',
	        success: function (data) {
	        	var list = data.data;
	        	renderData(list);
	        }
	    });
	}
	//打印页面预览
function printpreview() {
    var WebBrowser = '<OBJECT ID="WebBrowser1" WIDTH=0 HEIGHT=0 CLASSID="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"></OBJECT>';
    document.getElementById("printdiv").style.display = "none";//隐藏打印及其打印预览页面
    document.body.insertAdjacentHTML('beforeEnd', WebBrowser); //在body标签内加入html（WebBrowser activeX控件）
    WebBrowser1.ExecWB(7, 1); //打印预览
}
	$(".print").click(function(){
		var newstr = document.getElementById("printContentDiv").innerHTML;   //获得需要打印的内容
            // alert(newstr);
            var oldstr = document.body.innerHTML;   //保存原先网页的代码
            document.body.innerHTML = newstr; //将网页内容更改成需要打印
            window.print();
            document.body.innerHTML = oldstr;   //将网页还原
            return false;
	})
// 	$(".addUser").click(function(data){
// 		var $checkbox = $('.vo_list tbody input[type="checkbox"][name="checked"]');
//		var $checked = $('.vo_list tbody input[type="checkbox"][name="checked"]:checked');
//		if($checkbox.is(":checked")){
//			var index = layer.msg('添加中，请稍候',{icon: 16,time:false,shade:0.8});
//					var orderArray = "";
//					for(var j=0;j<$checked.length;j++){
//          			var orderId = $checked.eq(j).attr("data-id");
//          			orderArray= orderArray+orderId+",";
//	            	}
//					orderArray = orderArray.substring(0,orderArray.length-1);
//			//是否添加过信息
// 		var dto = {
// 			"deliveryId":$(".deliveryMan").val(),
// 			"orderIds":orderArray
// 		};
//	 	$.ajax({
//			        url: API_URL_HEAD+"outPutBills/create",
//			        type: 'post',
//			        async: false,
//			        data: JSON.stringify(dto),
//			        contentType: 'application/json; charset=utf-8',
//			        dataType: 'json',
//			        success: function (data) {
//			        	if(data.code == 0){
//			        		layer.close(index);
//		              		 //刷新父页面
//			 				parent.location.reload();
//		            	}
//			        }
//			  });
//		}
// 		return false;
// 	})
 	//全选
	form.on('checkbox(allChoose)', function(data){
		var child = $(data.elem).parents('table').find('tbody input[type="checkbox"]:not([name="show"])');
		child.each(function(index, item){
			item.checked = data.elem.checked;
		});
		form.render('checkbox');
	});

	//通过判断文章是否全部选中来确定全选按钮是否选中
	form.on("checkbox(choose)",function(data){
		var child = $(data.elem).parents('table').find('tbody input[type="checkbox"]:not([name="show"])');
		var childChecked = $(data.elem).parents('table').find('tbody input[type="checkbox"]:not([name="show"]):checked')
		if(childChecked.length == child.length){
			$(data.elem).parents('table').find('thead input#allChoose').get(0).checked = true;
		}else{
			$(data.elem).parents('table').find('thead input#allChoose').get(0).checked = false;
		}
		form.render('checkbox');
	})
//生成select options
	function createSelectOptions(selObj,dataArray,id){
		for(var i in dataArray){
			var every = dataArray[i];
			if(id==undefined){
				$(selObj).append("<option value='"+every["id"]+"'>"+every["deliveryName"]+"</option>");
			}else{
				if(every["id"]==id){
					$(selObj).append("<option value='"+every["id"]+"'  selected=\"selected\">"+every["deliveryName"]+"</option>");
				}else{
					$(selObj).append("<option value='"+every["id"]+"'>"+every["deliveryName"]+"</option>");
				}
			}
			
		}
	}	
})

//格式化时间
function formatTime(_time){
	var date = new Date();
	date.setTime(_time);
    if(_time && _time!=null && _time !="")
    {
    	var year = date.getFullYear();
	    var month = date.getMonth()+1<10 ? "0"+(date.getMonth()+1) : date.getMonth()+1;
	    var day = date.getDate()<10 ? "0"+date.getDate() : date.getDate();
	    var hour = date.getHours()<10 ? "0"+date.getHours() : date.getHours();
	    var minute = date.getMinutes()<10 ? "0"+date.getMinutes() : date.getMinutes();
	    return year+"-"+month+"-"+day+" "+hour+":"+minute;
    }else
    {
    	return "";
    }
}
//获取url中的参数
function getQueryString(name) {
  var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); // 匹配目标参数
  var result = window.location.search.substr(1).match(reg); // 对querystring匹配目标参数
  if (result != null) {
    return decodeURIComponent(result[2]);
  } else {
    return null;
  }
}